package org.huangrui.spark.scala.core.acc

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD
import org.apache.spark.util.LongAccumulator

/**
 * @Author hr
 * @Create 2024-10-19 21:44
 */
object Spark02_Acc {
  def main(args: Array[String]): Unit = {
    val sparConf = new SparkConf().setMaster("local").setAppName("WordCount")
    val sc = new SparkContext(sparConf)
    val rdd: RDD[Int] = sc.makeRDD(List(1, 2, 3, 4))
    // 获取系统累加器
    // Spark默认就提供了简单数据聚合的累加器
    val sumAcc: LongAccumulator = sc.longAccumulator("sum")
    //sc.doubleAccumulator
    //sc.collectionAccumulator
    rdd.foreach(
      // 使用累加器
      num => sumAcc.add(num)
    )
    // 获取累加器的值
    println("sum = " + sumAcc.value)
    sc.stop()
  }
}
